package com.alquiler.controllers;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.util.Callback;

import com.alquiler.dto.ItemDto;
import com.alquiler.entities.Cancha;
import com.alquiler.entities.Empleado;
import com.alquiler.entities.Reserva;
import com.alquiler.entities.Sede;
import com.alquiler.implementation.IScreenParent;
import com.alquiler.services.CanchaService;
import com.alquiler.services.ReservaService;
import com.alquiler.services.SedeService;
import com.alquiler.utils.ListaUtil;

public class ReporteCanchaController implements Initializable, IScreenParent {
	
	MainController mainController;	
	
	private Integer indexTabla;
	
	private ObservableList<Reserva> reservas;
	
	@FXML
    private Label result;

    @FXML
    private TableView<Reserva> tablaReservas;

    @FXML
    private TableColumn<Reserva, String> sedeColum;

    @FXML
    private ComboBox<ItemDto> sedesCombo;

    @FXML
    private Button buscar;

    @FXML
    private TableColumn<Reserva, String> fechaColum;

    @FXML
    private TableColumn<Reserva, String> indexColum;
    
    @FXML
    private TableColumn<Reserva, String> HoraInicialColum;
    
    @FXML
    private TableColumn<Reserva, String> HoraFinalColum;
    
    @FXML
    private TableColumn<Reserva, String> ValorColum;
    
    @FXML
    private TableColumn<Reserva, String> usuarioColum;
    
    @FXML
    private TableColumn<Reserva, String> identificacionColum;

    @FXML
    private ComboBox<ItemDto> canchasCombo;

    @FXML
    void consultarReservas(ActionEvent event) {
    	
    	if(canchasCombo.getSelectionModel().getSelectedItem() != null){  	
    		
    		Cancha cancha = new CanchaService().findById(canchasCombo.getSelectionModel().getSelectedItem().getId());
    	
    		loadListReservas(cancha.getIdCancha());  	
    		
    		result.setText("Reservas encontradas de la cancha "+ cancha.getNombre());
    		
    	}else{    		
    		result.setText("No se ha seleccionado ninguna cancha para realizar la consulta");
    	}
    	
    }

    @FXML
    void buscarChanchas(ActionEvent event) {
    	
    	ListaUtil.fillComboWithItems(canchasCombo, new CanchaService().getCanchasBySede(sedesCombo.getSelectionModel().getSelectedItem().getId()));

    }

	@Override
	public void setScreenParent(MainController screenPage) {
		mainController = screenPage;
		
	}

	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {
		
		ListaUtil.fillComboWithItems(sedesCombo, new SedeService().getListItems());	
		
		initializeTableSedes();
	}
	
	
	
	
	public void initializeTableSedes(){
		
		ListaUtil.setIndexCellTable(indexColum);
		
		fechaColum.setCellValueFactory(new PropertyValueFactory<Reserva, String>("fecha"));
		HoraInicialColum.setCellValueFactory(new PropertyValueFactory<Reserva, String>("horaInicial"));
		HoraFinalColum.setCellValueFactory(new PropertyValueFactory<Reserva, String>("horaFinal"));
		ValorColum.setCellValueFactory(new PropertyValueFactory<Reserva, String>("valor"));
		
		usuarioColum.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Reserva, String>, ObservableValue<String>>() {
		    @Override
		    public ObservableValue<String> call(TableColumn.CellDataFeatures<Reserva, String> p) {
		        if (p.getValue() != null) {
		            return new SimpleStringProperty(p.getValue().getUsuario().getNombres() + " " +p.getValue().getUsuario().getApellidos());
		        } else {
		            return new SimpleStringProperty("");
		        }
		    }
		});	
		
		identificacionColum.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Reserva, String>, ObservableValue<String>>() {
		    @Override
		    public ObservableValue<String> call(TableColumn.CellDataFeatures<Reserva, String> p) {
		        if (p.getValue() != null) {
		            return new SimpleStringProperty(((Integer)p.getValue().getUsuario().getIndentificacion()).toString());
		        } else {
		            return new SimpleStringProperty("");
		        }
		    }
		});	
		
		this.loadListReservas(null);
	}
	
	public void loadListReservas(Integer idCancha){
		
		try {
			
			if(idCancha != null){
				reservas = FXCollections.observableArrayList(new ReservaService().getTopDiezReservasByCancha(idCancha));
				
			}else{				
				reservas = FXCollections.observableArrayList(new ReservaService().findAll());	
			}
			
					
			tablaReservas.setItems(null);
			tablaReservas.layout();
			tablaReservas.setItems(reservas);		
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	

}
